package com.yazi.mxz.person.controller.shop;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.jackson.result.PageResult;
import com.yazi.mxz.common.entity.beans.StoreRestock;
import com.yazi.mxz.common.entity.beans.StoreRestockGoods;
import com.yazi.mxz.common.entity.enums.StoreRestockGoodsType;
import com.yazi.mxz.common.service.StoreRestockGoodsService;
import com.yazi.mxz.common.service.StoreRestockService;
import com.yazi.mxz.person.jackson.filter.StoreRestockGoodsFilter;

@Controller("shop-storeRestockGoodsController")
@RequestMapping("/shop/storeRestockGoods")
public class StoreRestockGoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreRestockGoodsController.class);
	
	@Resource
	StoreRestockGoodsService storeRestockGoodsService;
	@Resource
	StoreRestockService storeRestockService;
	
	@RequestMapping("/list/json")
	@ResponseBody
	public PageResult<?> listJson(StoreRestockGoods storeRestockGoods, Searching si) {
		debug(log, "搜索进货单商品");
		PageResult<StoreRestockGoods> jsonResult = new PageResult<StoreRestockGoods>();
		jsonResult.setFilter(StoreRestockGoodsFilter.getWithStoreRestockAndStoreGoods());
		try {
			Page<StoreRestockGoods> page = storeRestockGoodsService.list(storeRestockGoods, si);
			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "搜索进货单商品异常");
			jsonResult.setErrorMessage("查询订货单商品异常，请联系管理员");
		}
		return jsonResult;
	}
	
	
	@RequestMapping("/storeRestockList/part_detail")
	public String storeRestockListPart(long restockId, Searching si, Model model) {
		debug(log, "查询进货单详情");
		
		StoreRestockGoodsType[] types = StoreRestockGoodsType.values();
		Page<StoreRestockGoods> page = null;
		StoreRestock sr = storeRestockService.one(restockId);
		
		if (null != sr && sr.getStore().equals(getStore(true))) {
			
			StoreRestockGoods srg = new StoreRestockGoods();
			srg.setStoreRestock(sr);
			
			page = storeRestockGoodsService.list(srg, si);
		}
		
		model.addAttribute("storeRestock", sr);
		model.addAttribute("list", page.getList());
		model.addAttribute("types", types);
		model.addAttribute("pi", si);
		model.addAttribute("restockId", restockId);
		return "shop/storeRestock_list_part_detail";
	}
	
}
